﻿using FreeRedis.Internal;
using FreeSql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace InfrastructureBase.Data
{
    public static class IdleBusExtesions
    {
        static AsyncLocal<string> asyncDb = new AsyncLocal<string>();
        public static IdleBus<IFreeSql> ChangeDatabase(this IdleBus<IFreeSql> ib, string db)
        {
            asyncDb.Value = db;
            return ib;
        }

        /// <summary>
        /// 设置默认
        /// </summary>
        /// <param name="ib"></param>
        /// <returns></returns>
        public static IFreeSql Get(this IdleBus<IFreeSql> ib) => ib.Get(asyncDb.Value ?? "ConnDB_Mysql");

        public static IBaseRepository<T> GetRepository<T>(this IdleBus<IFreeSql> ib) where T : class
            => ib.Get().GetRepository<T>();
    }
}
